<template>
<view class="u-wrap">
		<view class="u-search-box">
			<view class="u-search-inner" @click="GoUrl('../search/search')">
				<u-icon name="search" color="#909399" :size="28"></u-icon>
				<text class="u-search-text">搜商品</text>
			</view>
		</view>
		<view class="u-menu-wrap">
			<scroll-view scroll-y scroll-with-animation class="u-tab-view menu-scroll-view" :scroll-top="scrollTop">
				<view v-for="(item,index) in tabbar" :key="index" class="u-tab-item" :class="[current==index ? 'u-tab-item-active' : '']"
				 :data-current="index" @tap.stop="swichMenu(index,item.id)">
					<text class="u-line-1">{{item.name}}</text>
				</view>
			</scroll-view>
			<block v-for="(item,index) in tabbar" :key="index">
				<scroll-view scroll-y class="right-box" v-if="current==index">
					<view class="page-view">
						<view class="class-item">
							<view class="item-title">
								<text>{{item.fullname}}</text>
							</view>
							<view class="item-container">
								<view class="thumb-box" v-for="(item1, index1) in item.foods" :key="index1" @click="GoUrl('../shop_detail/shop_detail?pid='+item1.id)">
									<image class="item-menu-image" :src="item1.icon" mode=""></image>
									<view class="item-menu-name">{{item1.name}}</view>
								</view>
							</view>
						</view>
					</view>
				</scroll-view>
			</block>
		</view>
	</view>
</template>

<script setup lang='ts'>
import { ref,reactive } from 'vue'
import { onLoad,onShow } from '@dcloudio/uni-app'
/* import { classify } from "../../common/classify"; */
import { GoUrl,limtStrLen } from '@/utils/utils'
import { getFirst,getAllProducts } from '@/utils/apiFunction'	
import { classStore } from '@/stores/classStore'	
const classstore = classStore();


let tabbar=reactive<any>([]);
//let tabbar=reactive(classify); //模拟数据
let scrollTop=ref(0) //tab标题的滚动条位置
let current=ref(0)   // 预设当前项的值
let menuHeight=ref(0) // 左边菜单的高度
let menuItemHeight=ref(0) // 左边菜单item的高度


//分类类信息
let classifyData=reactive<any>({data:[]});  //添加多一层data:[]包裹，预防动态组件不渲染
//产品信息
let productsData=reactive<any>({data:[]});

onLoad(async ()=>{
	await getFirst().then(res=>{
		//console.log("第一分类",res)
		if(res.data.code===200){
			res.data.data.map((item,index)=>{
				var obj={};
				obj.id=item.id;
				obj.name=limtStrLen(item.firstclass,4); //超出4个字符隐藏
				obj.fullname=item.firstclass; //完整姓名
				//obj.foods=classify[index].foods; //模拟数据
				obj.foods=[];
				tabbar.push(obj);
			})
		}
	}); //获取主类
	getAllProducts().then(res=>{
		console.log("产品信息",res)
		if(res.data.code===200){
			productsData.data=res.data.data;
			//切换显示一次产品
			swichMenu(classstore.classIndex,classstore.classId);
	    }
	}); //获取产品信息	
});
//console.log("classifyData.............",classify)	
onShow(()=>{
	console.log("tabbar===>",tabbar,classstore.classIndex,classstore.classId)
	if(classstore.classIndex===0 && classstore.classId===0){
		console.log("默认赋值",productsData.data[0])
		classstore.classIndex=0;
		classstore.classId=productsData.data[0].firstid;
	}
	console.log("classstore.classIndex==>",classstore.classIndex,"classstore.classId==>",classstore.classId)
	swichMenu(classstore.classIndex,classstore.classId);
})
const getImg=()=>{
	return Math.floor(Math.random() * 35);
}
const swichMenu=async (index:number,firstClassId:number)=>{
		//if(index == current.value) return ;
		current.value = index;
		// 如果为0，意味着尚未初始化
		if(menuHeight.value == 0 || menuItemHeight.value == 0) {
			await getElRect('menu-scroll-view', 'menuHeight');
			await getElRect('u-tab-item', 'menuItemHeight');
		}
		// 将菜单菜单活动item垂直居中
		scrollTop.value = index * menuItemHeight.value + menuItemHeight.value / 2 - menuHeight.value / 2;
		
		
		//切换产品展示内容
		//显示对应的产品内容
		let filterData=productsData.data.filter(item => item.firstid == firstClassId);
		console.log("过滤的产品内容:",filterData);
		tabbar[index].foods=[];
		filterData.forEach(item=>{
			//console.log(item);
			var obj={};
			obj.id=item.id;
			obj.name=item.productname;
			obj.key=item.productname;
			obj.icon=item.productimg;
			obj.cat=10;
			tabbar[index].foods.push(obj);
		});
		console.log("最终显示产品:",tabbar[index].foods);
}
// 获取一个目标元素的高度
const getElRect=(elClass:string, dataVal:string)=>{
	new Promise((resolve, reject) => {
		const query = uni.createSelectorQuery().in(this);
		query.select('.' + elClass).fields({size: true},res => {
			console.log("节点信息res===>",res)
			// 如果节点尚未生成，res值为null，循环调用执行
			if(!res) {
				setTimeout(() => {
					getElRect(elClass,dataVal);
				}, 10);
				return ;
			}
			[dataVal] = res?.height;
		}).exec();
	})
}
</script>

<style lang='scss' scoped>
	@import "class.scss";
</style>